package com.pms.actions;

import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pms.businesslogic.BusinessDelegator;
import com.pms.businesslogic.LogicFactory;
import com.pms.platform.dto.User;
import com.pms.utils.Converter;
import com.pms.utils.ValueObject;
import com.pms.framework.JSPUtility;

public class CreateAction extends FillComboAction{
	public void execute(HttpServletRequest request, HttpServletResponse response) {

		Hashtable error = (Hashtable) request.getAttribute("error");
		HttpSession session =request.getSession();

		if(error!=null && error.size() > 0){
			return;
		}else{
			error=new Hashtable();
		}
		try{
			ValueObject outValObj=null;
			ValueObject inValObj=Converter.convertToValueObject(request);

			String nextPageToken="success";
			User user=(User)session.getAttribute("user");
			inValObj.put("user", user);

			int actionEvent =JSPUtility.GetInt(request,"actionEvent");
			long preveRequestTracker=Long.parseLong(request.getParameter("prevRequestTracker").toString());

			if(session.getAttribute("prevRequestTracker")==null || Long.parseLong(session.getAttribute("prevRequestTracker").toString())!=preveRequestTracker){
				System.out.println(" @@@@@@@@@ going for creation ");

				BusinessDelegator blObj= LogicFactory.getInstance().getBusinessLogic(actionEvent);
				outValObj=blObj.create(inValObj);
				session.setAttribute("prevRequestTracker", preveRequestTracker);
				outValObj.transferToRequest(request);
				
				if(outValObj!=null&&outValObj.get("error")!=null){
					error.put("error", outValObj.get("error").toString());
					request.setAttribute("error", error);
					nextPageToken="failure";
				}

			}else{
				System.out.println(" @@@@@@@@@ page has been refreshed.");
			}

			long milliSec= System.currentTimeMillis();
			request.setAttribute("prevRequestTracker", milliSec);
			super.execute(request, response);
			
			request.setAttribute("nextPageToken", nextPageToken);


		}catch (Exception e) {
			e.printStackTrace();
		}

	}

}
